package com.yanqu.road.dao.impl.activity.flowerstudy;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.flowerstudy.data.FlowerStudyUserData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class FlowerStudyUserDataDaoImpl extends TempDataObjectDao<FlowerStudyUserData> {
    public static String getFlowerStudyUserDataSql = "select * from `t_u_flower_study_activity_user` where `activityId`=? and `userId`=?";
    public FlowerStudyUserData getFlowerStudyUserData(int activityId, long userId) {
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getUserDb());
            call = getDb().getConnection().prepareCall(getFlowerStudyUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setLong(i++, userId);
            rs = call.executeQuery();
            if (rs.next()) {
                return parse(rs);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return null;
    }

    public static String updateFlowerStudyUserDataSql = "update `t_u_flower_study_activity_user` set visitManId=?,visitManOpCount=?,visitManRefreshTime=?,poemRecord=?,poemRefreshTime=?,poemTodayFinishCount=?,poemIsSubmitted=?,hasShare=? where activityId=? and userId=?";
    @Override
    protected boolean updateData(FlowerStudyUserData flowerStudyUserData) {
        flowerStudyUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserData.getVisitManId()));
        para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserData.getVisitManOpCount()));
        para.put(i++, new DbParameter(Types.BIGINT, flowerStudyUserData.getVisitManRefreshTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(flowerStudyUserData.getPoemRecord())));
        para.put(i++, new DbParameter(Types.BIGINT, flowerStudyUserData.getPoemRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserData.getPoemTodayFinishCount()));
        para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserData.getPoemIsSubmitted()));
        para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserData.getHasShare()));
        para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, flowerStudyUserData.getUserId()));
        if (getDb().executeNoneQuery(updateFlowerStudyUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertFlowerStudyUserDataSql = "insert into `t_u_flower_study_activity_user`(activityId,userId,visitManId,visitManOpCount,visitManRefreshTime,poemRecord,poemRefreshTime,poemTodayFinishCount,poemIsSubmitted,hasShare) values (?,?,?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(FlowerStudyUserData flowerStudyUserData) {
        flowerStudyUserData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getUserDb());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, flowerStudyUserData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserData.getVisitManId()));
        para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserData.getVisitManOpCount()));
        para.put(i++, new DbParameter(Types.BIGINT, flowerStudyUserData.getVisitManRefreshTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(flowerStudyUserData.getPoemRecord())));
        para.put(i++, new DbParameter(Types.BIGINT, flowerStudyUserData.getPoemRefreshTime()));
        para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserData.getPoemTodayFinishCount()));
        para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserData.getPoemIsSubmitted()));
        para.put(i++, new DbParameter(Types.INTEGER, flowerStudyUserData.getHasShare()));
        if (getDb().executeNoneQuery(insertFlowerStudyUserDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected FlowerStudyUserData parseData(ResultSet rs) throws SQLException {
        FlowerStudyUserData flowerStudyUserData = new FlowerStudyUserData();
        flowerStudyUserData.setActivityId(rs.getInt("activityId"));
        flowerStudyUserData.setUserId(rs.getLong("userId"));
        flowerStudyUserData.setVisitManId(rs.getInt("visitManId"));
        flowerStudyUserData.setVisitManOpCount(rs.getInt("visitManOpCount"));
        flowerStudyUserData.setVisitManRefreshTime(rs.getLong("visitManRefreshTime"));
        flowerStudyUserData.setPoemRecord(JSON.parseObject(rs.getString("poemRecord"), new TypeReference<ConcurrentHashMap<Integer, Integer>>() {}));
        flowerStudyUserData.setPoemRefreshTime(rs.getLong("poemRefreshTime"));
        flowerStudyUserData.setPoemTodayFinishCount(rs.getInt("poemTodayFinishCount"));
        flowerStudyUserData.setPoemIsSubmitted(rs.getInt("poemIsSubmitted"));
        flowerStudyUserData.setHasShare(rs.getInt("hasShare"));

        return flowerStudyUserData;
    }
}
